package com.jyb.qiyefuwu.view.adpater;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jyb.qiyefuwu.BR;
import com.jyb.qiyefuwu.R;
import com.jyb.qiyefuwu.entity.NeedFollowListBean;
import com.swgk.core.customview.RoundImageView;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;


/**
 * 作者:陈方凯
 * 日期:2021/09/17
 * 说明:需求item
 */
public class NeedFollowListAdapter extends BaseRecycleAdapter<NeedFollowListBean> {
    RequestOptions options = new RequestOptions().placeholder(R.mipmap.icon_default_company)//图片加载出来前，显示的图片
            .fallback(R.mipmap.icon_default_company) //url为空的时候,显示的图片
            .error(R.mipmap.icon_default_company);//图片加载失败后，显示的图片

    RequestOptions personOptions = new RequestOptions().placeholder(R.mipmap.morentu_person)//图片加载出来前，显示的图片
            .fallback(R.mipmap.morentu_person) //url为空的时候,显示的图片
            .error(R.mipmap.morentu_person);//图片加载失败后，显示的图片

    RequestOptions teamOptions = new RequestOptions().placeholder(R.mipmap.icon_default_team)//图片加载出来前，显示的图片
            .fallback(R.mipmap.icon_default_team) //url为空的时候,显示的图片
            .error(R.mipmap.icon_default_team);//图片加载失败后，显示的图片

    public NeedFollowListAdapter(Context context, List<NeedFollowListBean> beans) {
        super(context, R.layout.need_follow_item, beans);
    }

    @Override
    public void convert(ItemViewHolder holder, NeedFollowListBean item) {
        holder.setBinding(BR.viewModel, item);

        String KeyWord;
        String s = item.offerPerson + "";
        if (s.length() == 5) {
            KeyWord = getString(s, 1, 2, 3);
        } else if (s.length() == 6) {
            KeyWord = getString(s, 2, 3, 4);
        } else if (s.length() == 7) {
            KeyWord = getString(s, 3, 4, 5);
        } else if (s.length() == 8) {
            KeyWord = getString(s, 4, 5, 6);
        } else {
            KeyWord = item.offerPerson + "";
        }
        if (!TextUtils.isEmpty(item.servicePrincipal) && item.requirementTypes > 4) {
            holder.getView(R.id.image_q).setVisibility(item.servicePrincipal.indexOf("1") > -1 ? View.VISIBLE : View.GONE);
            holder.getView(R.id.image_t).setVisibility(item.servicePrincipal.indexOf("2") > -1 ? View.VISIBLE : View.GONE);
//            Log.e("showlogo:"+ UiUtil.getTime(item.updateTime), holder.getView(R.id.image_q).getVisibility()+"___"+holder.getView(R.id.image_t).getVisibility());
        } else {
            holder.getView(R.id.image_q).setVisibility(View.GONE);
            holder.getView(R.id.image_t).setVisibility(View.GONE);
        }
        if (item.offerStatus == 1) {
            Glide.with(context).load(R.mipmap.icon_baojiazhong).into((ImageView) holder.getView(R.id.image_state_baojia));
        } else if (item.offerStatus == 2) {
            Glide.with(context).load(R.mipmap.icon_yijieshu).into((ImageView) holder.getView(R.id.image_state_baojia));
        }
        if (item.publishType == 1) {
            ((ImageView) holder.getView(R.id.iv_need_item_logo)).setVisibility(View.VISIBLE);
            ((RoundImageView) holder.getView(R.id.riv_need_logo)).setVisibility(View.GONE);
            Glide.with(context).load(item.image).apply(options).into((ImageView) holder.getView(R.id.iv_need_item_logo));
        } else if (item.publishType == 2) {
            ((ImageView) holder.getView(R.id.iv_need_item_logo)).setVisibility(View.GONE);
            ((RoundImageView) holder.getView(R.id.riv_need_logo)).setVisibility(View.VISIBLE);
            Glide.with(context).load(item.image).apply(teamOptions).into((RoundImageView) holder.getView(R.id.riv_need_logo));
        } else {
            ((ImageView) holder.getView(R.id.iv_need_item_logo)).setVisibility(View.GONE);
            ((RoundImageView) holder.getView(R.id.riv_need_logo)).setVisibility(View.VISIBLE);
            Glide.with(context).load(item.image).apply(personOptions).into((RoundImageView) holder.getView(R.id.riv_need_logo));
        }

        holder.setText(R.id.tv_need_type, item.requirementTypes == 1 ? "商标需求" : item.requirementTypes == 2 ? "专利需求" :
                item.requirementTypes == 3 ? "版权需求" : item.requirementTypes == 5 ? "服务需求" : "需求")
                .setText(R.id.tv_count_all, KeyWord);
        holder.getView(R.id.iv_focus).setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                iFollow.cancelFollow(item,holder.getIndex_position());
            }
        });
    }

    private String getString(String s, int i2, int i3, int i4) {
        String KeyWord;
        int pos1 = Integer.parseInt(s.substring(0, i2));
        int pos2 = Integer.parseInt(s.substring(i2, i3));
        int pos3 = Integer.parseInt(s.substring(i3, i4));
        if (pos3 >= 5) pos2++;
        if (pos2 == 10) {
            pos1++;
        }
        KeyWord = pos2 == 10 ? pos1 + ".0w" : pos1 + "." + pos2 + "w";
        return KeyWord;
    }
    private IFollow iFollow;

    public interface IFollow{
       void cancelFollow(NeedFollowListBean item,int position);
    }

    public void setOnClickFollow(IFollow iFollow){
        this.iFollow = iFollow;
    }
}
